﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SEMS.Models;
using SEMS.DAL;
using System.Data;

namespace SEMS.BLL
{
    public class ExcelBS
    {
        ///<summary>
        ///将学生成绩导出到EXCEL表
        ///</summary>
        ///<return>成功则返回true</return>
        public static bool CreateExcel (string class_id)
        {
            try
            {
                var systeminfo=BLL.SysinfoBS.GetSysinfo();
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                excel.Application.Workbooks.Add(true);
                
                excel.Cells[1, 1] = "学号";
                excel.Cells[1, 2] = "姓名";
                for (int i = 1 ; i <= 5 ; i++)
                {
                    excel.Cells[1, i + 2] = "M" + i;
                }
                var students = SEMS.BLL.StudentBS.GetStudentList(class_id);
                int row = 2;
                foreach (var item in students)
                {
                    int column = 1;
                    excel.Cells[row, column++] = item.student_id;
                    excel.Cells[row, column++] = item.student_name;
                    for (int i = 1 ; i <= 5 ; i++)
                    {
                        string module_id="M"+i;
                        excel.Cells[row, column++] =
                            BLL.Module_scoreBS.GetMoudle_score
                            (item.student_id, module_id, systeminfo.sysinfo_school_year, systeminfo.sysinfo_semester);
                    }
                }
                //excel.SaveWorkspace(@"D:\test.xls");
                excel.Visible = true;
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}